package at.fhhgb.mc.android.mfb_hgb.activity.dialog;

import java.util.Calendar;

import android.app.DatePickerDialog.OnDateSetListener;
import android.app.Dialog;
import android.app.DialogFragment;
import android.os.Bundle;
import android.text.format.DateFormat;
import android.widget.DatePicker;
import android.widget.EditText;

public class DatePickerDialog extends DialogFragment implements OnDateSetListener {
	 @Override
	    public Dialog onCreateDialog(Bundle savedInstanceState) {
	        // Use the current date as the default date in the picker
	        int year;
	        int month;
	        int day;
	        
	        if(getArguments() != null && getArguments().containsKey("defaultDate"))
	        {
	        	Calendar defaultDate = (Calendar)getArguments().getSerializable("defaultDate");
	        	
	        	year = defaultDate.get(Calendar.YEAR);
	        	month = defaultDate.get(Calendar.MONTH);
	        	day = defaultDate.get(Calendar.DAY_OF_MONTH);
	        }
	        else
	        {
	        	Calendar c = Calendar.getInstance();
	        	
	        	year = c.get(Calendar.YEAR);
	        	month = c.get(Calendar.MONTH);
	        	day = c.get(Calendar.DAY_OF_MONTH);
	        }

	        // Create a new instance of DatePickerDialog and return it
	        return new android.app.DatePickerDialog(getActivity(), this, year, month, day);
	    }

		@Override
		public void onDateSet(DatePicker arg0, int year, int month, int day) {
			Calendar selectedDate = Calendar.getInstance();
			selectedDate.set(year, month, day);
			
			java.text.DateFormat dateFormat = DateFormat.getDateFormat(getActivity());	
			
			EditText time = (EditText)getActivity().findViewById(getArguments().getInt("editTextKey"));
			time.setText(dateFormat.format(selectedDate.getTime()));
		}
}
